﻿#include "protreewidget.h"
#include "protreeitem.h"
#include "const.h"
#include <QDir>
#include <QDebug>
#include <QTreeWidgetItem>
#include <QHeaderView>
#include <QGuiApplication>
#include <QMenu>
#include <QFileDialog>
ProTreeWidget::ProTreeWidget(QWidget *parent):QTreeWidget (parent)
{
    qRegisterMetaType<QVector<int> >("QVector<int>");
    //隐藏表头
    this->header()->hide();

    //生成右键行为对象
    _action_import = new QAction(QIcon(":/icon.import.png"),tr("导入文件"),this);

    connect(_action_import,&QAction::triggered,this,&ProTreeWidget::slot_import);

}

void ProTreeWidget::addProToTree(const QString name, const QString path)
{

    QDir dir(path);
    QString file_path = dir.absoluteFilePath(name);

    if(m_filePaths.find(file_path)!=m_filePaths.end())   //说明文件已存在当前的列表中
    {
         qDebug() << "file has loaded" << endl;
         return;
    }

    //构造项目用的文件夹
    QDir pro_dir(file_path);
    //如果文件夹不存在则创建
    if(!pro_dir.exists()){
        bool enable = pro_dir.mkpath(file_path);
        if(!enable){
            qDebug() << "pro_dir make path failed" << endl;
            return;
        }
    }
    m_filePaths.insert(file_path);

    auto * item = new ProTreeItem(this, name, file_path,TreeItemPro);
    item->setData(0,Qt::DisplayRole, name);
    item->setData(0,Qt::DecorationRole, QIcon(":/icon/dir.png"));
    item->setData(0,Qt::ToolTipRole, file_path);
    //绑定单击item的信号   itemPressed(QTreeWidgetItem *item, int column)
    connect(this,&ProTreeWidget::itemPressed,this,&ProTreeWidget::slot_item_press);
}

void ProTreeWidget::slot_item_press(QTreeWidgetItem *item, int column)
{
    if(QGuiApplication::mouseButtons() ==  Qt::RightButton)
    {
        QMenu right_menu(this);
        int itemtype = (int)item->type();
        if(itemtype ==TreeItemPro )
        {
            _right_btn_item = item;   //存当前右键点击的item
            right_menu.addAction(_action_import);
            right_menu.exec(QCursor::pos());   //菜单弹出位置为鼠标点击的位置
        }

    }
}

void ProTreeWidget::slot_import()
{
    QFileDialog file_dialog;
    file_dialog.setFileMode(QFileDialog::Directory);
    file_dialog.setWindowTitle("选择要导入的文件夹");
    if(!_right_btn_item)
    {
        qDebug()<<"_right_btn_item is null"<<endl;
        return;
    }
    QString file_path = dynamic_cast<ProTreeItem*>(_right_btn_item)->getPath();
    file_dialog.setDirectory(file_path);
    file_dialog.setViewMode(QFileDialog::Detail);
    QStringList fileNames;
    if(!file_dialog.exec())
    {
        qDebug()<<"file_dialog open is failed";
        return;
    }
    fileNames = file_dialog.selectedFiles();
    if(fileNames.length() <= 0)
    {
       return;
    }
    QString import_path = fileNames.at(0);
    qDebug()<<import_path;

    int file_count = 0;
    //创建模态对话框
    _dialog_progress = new QProgressDialog(this);

    //耗时操作放在线程中操作

    _thread_creat_pro = std::make_shared<ProTreeThread>(std::ref(import_path),std::ref(file_path),
                        _right_btn_item,
                        std::ref(file_count),this,_right_btn_item,nullptr);
    //连接更新进度框操作
    connect(_thread_creat_pro.get(),&ProTreeThread::SigUpdateProgress,this,
            &ProTreeWidget::SlotImport);


}

ProTreeWidget::~ProTreeWidget()
{

}
